package com.hz.friends.fragment.contacts;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hz.friends.activity.AddFriendsActivity;
import com.hz.friends.activity.R;
import com.hz.friends.activity.R.layout;
import com.hz.friends.activity.SkipFansActivity;
import com.hz.friends.fragment.BaseFragment;
import com.hz.friends.serverrequest.ALEngineConfig.CallBack;
import com.hz.friends.serverrequest.ALUserEngine;
import com.hz.friends.utils.MLog;

public class ContactsFragment extends BaseFragment implements
		View.OnClickListener {
	private Button btn_friends;
	private Button btn_group;
	private FriendContacts mfriendcontacts = null;
	private GroupContacts mgroupcontacts = null;
	private EditText edt_search;
	private LinearLayout layout_friendslabs;
	private TextView txv_fansnum;
	private TextView txv_concernsnum;
	private TextView txv_blacklistsnum;
	private final String TAG = "ContactsFragment";
	private RelativeLayout layout_search;
	private PullToRefreshListView userlistview;
	private PullToRefreshListView grouplistview;
	private LinearLayout layout_group;
	private LinearLayout layout_friend;
	private static int REQUEST_JUMP_REFREST_NUMS = 1;

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_contacts, null);
	}

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		init();
		initViews();
		setViews();
		initEvents();
	}

	@Override
	protected void init() {
		
	}

	@Override
	protected void initViews() {
		ImageButton imbtn_bar_right = (ImageButton) findViewById(R.id.imbtn_bar_right); // friends类别
		btn_friends = (Button) findViewById(R.id.btn_friends); // friends类别
		btn_group = (Button) findViewById(R.id.btn_group); // group类别
		userlistview = (PullToRefreshListView) findViewById(R.id.userlistview); // listview
		grouplistview = (PullToRefreshListView) findViewById(R.id.grouplistview); // listview
		layout_friend = (LinearLayout) findViewById(R.id.layout_friend); // listview
		layout_group = (LinearLayout) findViewById(R.id.layout_group); // listview
		edt_search = (EditText) findViewById(R.id.edt_search); // 搜索输入框
		layout_search = (RelativeLayout) findViewById(R.id.layout_search); // 搜索布局
		LinearLayout layout_fans = (LinearLayout) findViewById(R.id.layout_fans); // friends
		// 下的类别一
		LinearLayout layout_concern = (LinearLayout) findViewById(R.id.layout_concern); // friends
		// 下的类别二
		LinearLayout layout_blacklist = (LinearLayout) findViewById(R.id.layout_blacklist); // friends
		// 下的类别三
		layout_friendslabs = (LinearLayout) findViewById(R.id.layout_friendslabs); // friends
																					// 下的包含三类别布局

		txv_fansnum = (TextView) layout_fans.findViewById(R.id.name);
		txv_concernsnum = (TextView) layout_concern.findViewById(R.id.name);
		txv_blacklistsnum = (TextView) layout_blacklist.findViewById(R.id.name);

		layout_fans.setOnClickListener(this);
		layout_concern.setOnClickListener(this);
		layout_blacklist.setOnClickListener(this);
		imbtn_bar_right.setOnClickListener(this);
		
		mfriendcontacts = new FriendContacts(userlistview, getActivity(),1);
		mgroupcontacts = new GroupContacts(grouplistview, getActivity());
		initFriendsNums();
		
	}

	
	@Override
	protected void setViews() {

	}

	@Override
	protected void initEvents() {
		btn_group.setOnClickListener(this);
		btn_friends.setOnClickListener(this);
		edt_search.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_SEARCH) { // 搜索
					String serachtxt = edt_search.getText().toString();
					if (!TextUtils.isEmpty(serachtxt)) { // 搜索
						layout_friendslabs.setVisibility(View.GONE);
						mfriendcontacts.setType( -1);
						mfriendcontacts.onSearch(serachtxt);
					} else {
						showSysToast("请输入搜索关键字");
					}
				}
				return false;
			}
		});
	}

	@Override
	public void initDatas() {
//		if(mfriendcontacts.getType() != -3)
//			return;
//		initFriendsNums();
//		mfriendcontacts.setType(1);
//		mfriendcontacts.loadUserlist();   
	
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.imbtn_bar_right:
			startActivity(AddFriendsActivity.class);
			break;
		case R.id.btn_friends:
			layout_friendslabs.setVisibility(View.VISIBLE);
			layout_search.setVisibility(View.VISIBLE);
			btn_friends.setTextColor(Color.parseColor("#ffffff"));
			btn_group.setTextColor(Color.parseColor("#52c43c"));
			btn_friends
					.setBackgroundResource(R.drawable.shape_contanct_friends2);
			btn_group.setBackgroundResource(R.drawable.shape_contanct_group1);
			mfriendcontacts.setType(1);
			mfriendcontacts.loadUserlist();  
			layout_friend.setVisibility(View.VISIBLE);
			layout_group.setVisibility(View.INVISIBLE);
			initFriendsNums();
			break;
		case R.id.btn_group:
			btn_group.setTextColor(Color.parseColor("#ffffff"));
			btn_friends.setTextColor(Color.parseColor("#52c43c"));
			btn_group.setBackgroundResource(R.drawable.shape_contanct_group2);
			btn_friends
					.setBackgroundResource(R.drawable.shape_contanct_friends1);
			layout_friend.setVisibility(View.INVISIBLE);
			layout_group.setVisibility(View.VISIBLE);
//            if(mgroupcontacts == null)
//			mgroupcontacts = new GroupContacts(mlistview, getActivity());
//            else
//            	mgroupcontacts.loadData();
			break;
		case R.id.layout_fans:
//			grouplistview.setVisibility(View.GONE);
//			userlistview.setVisibility(View.VISIBLE);
//			mfriendcontacts.setType(0);
//			mfriendcontacts.loadUserlist();  
			Intent fansintent = new Intent(getActivity(), SkipFansActivity.class);
			fansintent.putExtra("type", 0);
			getActivity().startActivityForResult(fansintent, ContactsFragment.REQUEST_JUMP_REFREST_NUMS);
			break;
		case R.id.layout_concern:
//			grouplistview.setVisibility(View.GONE);
//			userlistview.setVisibility(View.VISIBLE);
//			mfriendcontacts.setType(1);
//			mfriendcontacts.loadUserlist();  
			Intent concernintent = new Intent(getActivity(), SkipFansActivity.class);
			concernintent.putExtra("type", 1);
			getActivity().startActivityForResult(concernintent, ContactsFragment.REQUEST_JUMP_REFREST_NUMS);
			break;
		case R.id.layout_blacklist:
//			grouplistview.setVisibility(View.GONE);
//			userlistview.setVisibility(View.VISIBLE);
//			mfriendcontacts.setType(2);
//			mfriendcontacts.loadUserlist();  
			Intent concernintentintent = new Intent(getActivity(), SkipFansActivity.class);
			concernintentintent.putExtra("type", 2);
			getActivity().startActivityForResult(concernintentintent, ContactsFragment.REQUEST_JUMP_REFREST_NUMS);
			break;
		default:
			break;
		}
	}

	public void initFriendsNums() {
		// 互粉数
		ALUserEngine.defauleEngine().getFollowerAndFolloweeCount(
				AVUser.getCurrentUser(), new CallBack() {

					@Override
					public void done(Object object, AVException avexception) {
						if (avexception == null) {
							  Integer num = (Integer) object;
							  txv_concernsnum.setText(String.valueOf(num));
						} else {
							avexception.printStackTrace();
						}
					}
				});
		// 用户粉丝数
		ALUserEngine.defauleEngine().getFollowerCount(AVUser.getCurrentUser(),
				new CallBack() {

					@Override
					public void done(Object object, AVException avexception) {
						if (avexception == null) {
							  Integer num = (Integer) object;
                              txv_fansnum.setText(String.valueOf(num));
						} else {
							avexception.printStackTrace();
						}
					}
				});

		// 拉黑数
		ALUserEngine.defauleEngine().getMyBanCount(
				new CallBack() {

					@Override
					public void done(Object object, AVException avexception) {
						if (avexception == null) {
							  Integer num = (Integer) object;
                              txv_blacklistsnum.setText(String.valueOf(num));
						} else {
							avexception.printStackTrace();
						}
					}
				});
		
//		// 用户关注数
//		ALUserEngine.defauleEngine().getFolloweeCount(AVUser.getCurrentUser(),
//				new CallBack() {
//
//					@Override
//					public void done(Object object, AVException avexception) {
//						if (avexception == null) {
//							  Integer num = (Integer) object;
//                              txv_concernsnum.setText(String.valueOf(num));
//						} else {
//							avexception.printStackTrace();
//						}
//					}
//				});
		
	}

 
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		MLog.i(TAG, "====contactsFragment  onActivityResult"+resultCode);
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == REQUEST_JUMP_REFREST_NUMS){//需要刷新顶部数值
			initFriendsNums();
		}
	}

}
